package com.suep.course.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.suep.course.dto.CourseDto;
import com.suep.course.dto.addCourseRequestDto;
import com.suep.course.dto.updateCourseRequestDto;
import com.suep.course.model.CourseManagement;
import com.suep.course.mapper.CourseManagementMapper;
import com.suep.course.repository.CourseRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class CourseManagementService {
    private final CourseRepository courseRepository;
    private final CourseManagementMapper courseMapper;
    public CourseDto getCourseById(Integer id){
        Objects.requireNonNull(id,"id cannot be null");

        //build mybatis-plus query condition
        Wrapper<CourseManagement> query= Wrappers.<CourseManagement>lambdaQuery().eq(CourseManagement::getId,id);

        //fetch by query condition
        return courseRepository.SelectFromId(query).stream().
                map(courseMapper::toCourseDto).
                findFirst()
                .orElse(null);
    }
    //获取所有教师信息
    public List<CourseDto> getAllCourses(){
        return courseRepository.SelectAll().stream().
                map(courseMapper::toCourseDto).
                toList();
    }
    //新增教师信息
    @Transactional
    public String addCourse(addCourseRequestDto addcourseRequestDto){
        //do validation
        Objects.requireNonNull(addcourseRequestDto,"addCourseRequestDto cannot be null");
        //build teacher object
        var targetCourse = CourseManagement.builder()
                .id(addcourseRequestDto.getId())
                .course_number(addcourseRequestDto.getCourse_number())
                .course_name(addcourseRequestDto.getCourse_name())
                .credit(addcourseRequestDto.getCredit())
                .class_hours(addcourseRequestDto.getClass_hours())
                .build();
        //save teacher object
        if (this.courseRepository.insert(targetCourse)<=0){
            throw new RuntimeException("Failed to add course");
        }else{
            return "success";
        }
    }
    //删除教师信息
    public void removeCourse(Integer id){
        //do validation
        Objects.requireNonNull(id,"id cannot be null");
        //do delete
        this.courseRepository.deleteById(id);
    }
    //更新教师信息
    @Transactional
    public String updateCourse(Integer id, updateCourseRequestDto updatecourseRequestDto){
        //do validation
        Objects.requireNonNull(updatecourseRequestDto,"updateCourseRequestDto cannot be null");
        //build teacher object
        var targetCourse = CourseManagement.builder().id(id).build();
        if(updatecourseRequestDto.getCredit()!=0){
            targetCourse.setCredit(updatecourseRequestDto.getCredit());
        }
        if(updatecourseRequestDto.getClass_hours()!=0){
            targetCourse.setClass_hours(updatecourseRequestDto.getClass_hours());
        }

        //update teacher object
        if (this.courseRepository.updateById(targetCourse)<=0){
            throw new RuntimeException("Failed to update courses");
        }else{
            return "success";
        }
    }
}
